//
//  HZCartTableViewCell.m
//  app
//
//  Created by 刘华舟 on 15/5/4.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZCartTableViewCell.h"
#import "HZTableView.h"
#import "HZImageSourceConstant.h"
#import "UIImageView+URL.h"
#import "UILabel+Conveniently.h"
#import "SNGiftAndPromotionView.h"

#import "MyToast.h"

#import "HZMallSqlDataManager.h"

static const CGFloat kHZGoodsTabFontRateSix = 1.100;
static const CGFloat kHZGoodsTabFontRatePlus = 1.100;

static const CGFloat kCartCellMargin = 24.0f;
static const CGFloat kCartGoodsPictureHeight = 160.0f;
static const CGFloat kCartCellSelectBtnWidth = 64.0f;
static const CGFloat kCartGoodsNumBtnWidth = 80.0f;
static const CGFloat kCartGoodsNumBtnHeight = 48.0f;

static const NSInteger kHZGoodsViewTeMaiBtnHeight = 48;
static const NSInteger kHZGoodsViewTeMaiBtnWidth = 180;

static const CGFloat kCartCellPadding = 20.0f;

@interface HZCartTableViewCell()

@property (strong, nonatomic) UIButton *selectBtn;
@property (strong, nonatomic) UIImageView *goodsPic;
@property (strong, nonatomic) UILabel *goodsTitleLabel;
@property (strong, nonatomic) UILabel *temaiLabel;
@property (strong, nonatomic) UILabel *goodsPriceLabel;
@property (strong, nonatomic) UIButton *goodsCountBtn;

@property (strong, nonatomic) SNGiftAndPromotionView *giftAndProView;

@end

@implementation HZCartTableViewCell


-(void)drawRect:(CGRect)rect
{
    //[self drawSeperatorLineLeft:[self.ubo uw:0] right:[self.ubo uw:0]];
    [self drawHorizonLineLeft:[self.ubo uw:0] right:[self.ubo uw:0] at:[self.ubo fh:UB_HEIGHT ph:-0.5] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
}

+ (instancetype)cellWithTableView:(HZTableView *)tableView type:(HZCartTableViewCellType)type{
    NSString *cellReuseIdentifier = @"HZCartTableViewCell";
    switch (type) {
        case HZCartTableViewCellTypeShoppingCart:
            cellReuseIdentifier = @"HZCartTableViewCellTypeShoppingCart";
            break;
            
        case HZCartTableViewCellTypeExchangeGoods:
            cellReuseIdentifier = @"HZCartTableViewCellTypeExchangeGoods";
            break;
        default:
            break;
    }
    HZCartTableViewCell * cell= (HZCartTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    if (cell == nil) {
        cell = [[HZCartTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier type:(HZCartTableViewCellType)type];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  type:(HZCartTableViewCellType)type{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentMode = UIViewContentModeRedraw;
        self.frame = CGRectOf([self.ufo fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:kCartGoodsPictureHeight + 2*kCartCellMargin]);
        //初始化全选按钮
        self.selectBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                     [self.ubo uw:kCartCellSelectBtnWidth fh:UB_HEIGHT])];
        [self.selectBtn setImage:[UIImage imageNamed:kImageMallCartGoodUnselect] forState:UIControlStateNormal];
        [self.selectBtn setImage:[UIImage imageNamed:kImageMallCartGoodSelected] forState:UIControlStateSelected];
        self.selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        self.selectBtn.backgroundColor = HZ_CLEAR_COLOR;
        [self.selectBtn addTarget:self action:@selector(selectBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.selectBtn];
        
        //初始化图片按钮
        self.goodsPic = [[UIImageView alloc] initWithFrame:CGRectOf([self.selectBtn.ufo fx:UB_RIGHT ux:kCartCellPadding fy:UB_TOP uy:kCartCellMargin],
                                                                    [self.ubo uw:kCartGoodsPictureHeight uh:kCartGoodsPictureHeight])];
        self.goodsPic.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
//        self.goodsPic.layer.masksToBounds = YES;
//        self.goodsPic.layer.cornerRadius = [self.ubo uw:4];
        self.goodsPic.layer.borderColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR.CGColor;
        self.goodsPic.layer.borderWidth = [self.ubo pw:2];
        //self.goodsPic.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.goodsPic];
        
        //初始化商品标题
        CGFloat goodsTitleWidth = [self.ubo fw:UB_WIDTH uw:-(kCartCellMargin + 2*kCartCellPadding + kCartGoodsPictureHeight +kCartCellSelectBtnWidth)];
        self.goodsTitleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPic.ufo fx:UB_RIGHT ux:kCartCellPadding fy:UB_TOP],
                                                                       [self.goodsPic.ubo ow:goodsTitleWidth fh:UB_HALF])];
        self.goodsTitleLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsTitleLabel setTextColor:HZ_MALL_TEXT_BLACK_COLOR];
        self.goodsTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self.goodsTitleLabel setFont:JKAppGlobleFontMenu];
        self.goodsTitleLabel.numberOfLines = 2;
        self.goodsTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.goodsTitleLabel];
        
        //初始化价格标签
        self.goodsPriceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPic.ufo fx:UB_RIGHT ux:kCartCellPadding fy:UB_BOTTOM uy:-kCartGoodsNumBtnHeight],
                                                                       [self.ubo fw:UB_WIDTH uw:-(kCartGoodsNumBtnWidth + kCartCellMargin +kCartCellPadding + kCartGoodsPictureHeight) uh:kCartGoodsNumBtnHeight])];
        self.goodsPriceLabel.textAlignment = NSTextAlignmentLeft;
        self.goodsPriceLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self addSubview:self.goodsPriceLabel];
        
        
        //初始化购买数量标签
        self.goodsCountBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ufo fx:UB_RIGHT ux:-(kCartGoodsNumBtnWidth+kCartCellMargin) oy:self.goodsPriceLabel.ufo.y],
                                                                      [self.ufo uw:kCartGoodsNumBtnWidth uh:kCartGoodsNumBtnHeight])];
        self.goodsCountBtn.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsCountBtn setTitleColor:HZ_MALL_TEXT_BLACK_COLOR forState:UIControlStateNormal];
        switch (type) {
            case HZCartTableViewCellTypeExchangeGoods:
            {
                
            }
                break;
            case HZCartTableViewCellTypeShoppingCart:
            {
                self.goodsCountBtn.layer.masksToBounds = YES;
                self.goodsCountBtn.layer.cornerRadius = [self.ufo uw:4];
                self.goodsCountBtn.layer.borderColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR.CGColor;
                self.goodsCountBtn.layer.borderWidth = [self.ufo pw:2];
                [self.goodsCountBtn addTarget:self action:@selector(countBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
            }
                break;
            default:
                break;
        }
        
        self.goodsCountBtn.titleLabel.font = JKAppGlobleFontSmallTagText;
        
        [self addSubview:self.goodsCountBtn];
        
        //商品店铺名称
        self.temaiLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsCountBtn.ufo fx:UB_LEFT ux:-kCartCellPadding-kHZGoodsViewTeMaiBtnWidth fy:UB_CENTER uy:-kHZGoodsViewTeMaiBtnHeight/2],
                                                                  [self.ubo uw:kHZGoodsViewTeMaiBtnWidth uh:kHZGoodsViewTeMaiBtnHeight])];
        self.temaiLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.temaiLabel setTextColor:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR];
        self.temaiLabel.textAlignment = NSTextAlignmentLeft;
        [self.temaiLabel setFont:kHZRFont_Tag_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];

        [self addSubview:self.temaiLabel];
        
        self.giftAndProView = [[SNGiftAndPromotionView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT uy:2*kSizeMallViewMargin+kCartGoodsPictureHeight],
                                                                                     [self.ubo fw:UB_WIDTH uh:0])];
        self.giftAndProView.hidden = YES;
        self.giftAndProView.targetCallerShowGiftList = [UBTargetCaller of:self action:@selector(giftAndProViewOnTouch)];
        [self addSubview:self.giftAndProView];
    }
    
    
    return self;
}

- (void)setModel:(HZCartCellModel *)model{
    _model = model;
    
    self.selectBtn.selected = self.model.isSelected;
    
    //[self.goodsPic setImageWithURL:self.model.picURL];
    
    //图像的动画特效
    [self.goodsPic sd_setImageWithURL:[NSURL URLWithString:model.picURL] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (cacheType == SDImageCacheTypeNone){
            self.goodsPic.alpha = 0.0f;
            [UIView animateWithDuration:0.5f animations:^{
                self.goodsPic.alpha = 1.0f;
            }];
        }
        
    }];
    
    self.goodsTitleLabel.text = self.model.goodsTitle;
    [self.goodsTitleLabel labelTextAlignTop];
    
   
    
    
    
    
    NSMutableAttributedString *atbTotalMoney = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.goodsPriceNow] attributes:nil];
    [atbTotalMoney addAttribute:NSForegroundColorAttributeName value:HZ_RED_COLOR range:NSMakeRange(0, model.goodsPriceNow.length+1)];
    [atbTotalMoney addAttribute:NSFontAttributeName value:JKAppGlobleFontMenu range:NSMakeRange(0, model.goodsPriceNow.length+1)];
    
    self.goodsPriceLabel.attributedText = atbTotalMoney;
    
    switch (model.cellType) {
        case HZCartTableViewCellTypeShoppingCart:
        {
            self.goodsCountBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.goodsCountBtn setTitle:model.goodsCountNum forState:UIControlStateNormal];
        }
            break;
        case HZCartTableViewCellTypeExchangeGoods:
        {
            self.goodsCountBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [self.goodsCountBtn setTitle:[NSString stringWithFormat:@"数量：%ld", (long)model.goodsCountNum] forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
    
    [self.goodsPriceLabel sizeToFit];
    
    self.temaiLabel.frame = CGRectOf([self.goodsPriceLabel.ufo fx:UB_RIGHT ux:kCartCellPadding fy:UB_CENTER uy:-kHZGoodsViewTeMaiBtnHeight/2],
                                                              [self.ubo uw:kHZGoodsViewTeMaiBtnWidth uh:kHZGoodsViewTeMaiBtnHeight]);
     self.temaiLabel.text = model.shopName;
    
    
    
    if (model.giftModel.gift) {
        self.giftAndProView.hidden = NO;
        [self.giftAndProView showViewWithOriginY:[self.ubo uh:2*kSizeMallViewMargin+kCartGoodsPictureHeight] giftModel:model.giftModel];
    }else{
        self.giftAndProView.hidden = YES;
    }
    
}



//点击选中
- (void)selectBtnOnClick{
    HZLog(@"点击cell选择按钮");
    
    
    self.model.selected = !self.model.isSelected;
    
    HZMallSqlDataManager *sqlDataManger = [HZMallSqlDataManager defaultManager];
    [sqlDataManger updateProductChoseStatuWithProductID:self.model.goodsID selected:self.model.isSelected];
    
    self.selectBtn.selected = self.model.isSelected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cartTableViewCellSelectedWithCell:)]) {
        [self.delegate cartTableViewCellSelectedWithCell:self];
    }
    
}

//点击数量按钮
- (void)countBtnOnClick{
    
    
    
    HZLog(@"点击更改数量按钮");
    __weak typeof(self) wself = self;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cartTableViewCellGoodsNumChangeWithCell: finished:)]) {
        [self.delegate cartTableViewCellGoodsNumChangeWithCell:self finished:^(NSString *result){
            wself.model.goodsCountNum = result;
            [wself.goodsCountBtn setTitle:result forState:UIControlStateNormal];
        }];
    }
}


- (void)giftAndProViewOnTouch{
    HZLog(@"点击赠品列表");
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cartTableViewCellGiftOnTouch:)]) {
        [self.delegate cartTableViewCellGiftOnTouch:self.model];
    }
}
@end
